package com.esisa.java.swing;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;



public class FormAjout extends JPanel implements ActionListener {
	
	private Form form1;
	private Form form2;
	
	private JButton save;
	private JButton nouveau;
	
	private Client client;
	private GestionClient gCleint;
	
	public FormAjout(GestionClient gc) {
		
		this.gCleint=gc;
		client=new Client();
		
		JPanel p=new JPanel();
		p.setLayout(new FlowLayout(FlowLayout.RIGHT));
		
		form1=new Form(new Produit());
		form2=new Form(new Client());
		
		save=new JButton("Save");
		nouveau=new JButton("New");
		save.addActionListener(this);
		nouveau.addActionListener(this);
		
		p.add(save);
		p.add(nouveau);
		
		setLayout(new BorderLayout());
		
		add(form1,"East");
		add(form2,"North");
		add(p,"South");
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		JButton b=(JButton)e.getSource();
		System.out.println(b.getText());
		switch (b.getText()) {
		
		case "Save":
		
			client.setCode(form2.getValue(0));
			client.setNom(form2.getValue(1));
			client.setPrenom(form2.getValue(2));
			gCleint.AddClient(client);
			gCleint.save();
			break;
		case "New":
			
			Integer code=new Integer(form1.getValue(0));
			Integer qte=new Integer(form1.getValue(3)) ;
			Double prix=new Double(form1.getValue(2));
	
			Produit p=new Produit(code,form1.getValue(1),prix,qte);
			
			System.out.println(p);
			client.getProduits().add(p);
			break;

		default:
			break;
		}
		
	}

}
